<%@page import="backpage.model.UploadHandler"%>
<%@page import="backpage.view.ResItem"%>
<%@page import="backpage.view.PageDivision"%>
<%@page import="com.model.User"%>
<%@page import="javassist.bytecode.stackmap.BasicBlock.Catch"%>
<%@page import="javax.xml.ws.RespectBinding"%>
<%@page import="java.net.*"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/backPage/";
String descripInAttr = "";		//the description from the onupload.servlet
String descripInParam = "";		//the description from error status of this page
if( request.getAttribute("description") != null)
	descripInAttr = java.net.URLEncoder.encode( (String)request.getAttribute("description") );
if( request.getParameter("descript") != null ){
	descripInParam = new String(request.getParameter("descript").getBytes("ISO8859_1"));
	descripInParam = descripInParam.replace("<br/>","\n");	//将<br/>还原为\n, 以在textarea域中显示
}

User user = (User) session.getAttribute("userInfo");
if (user == null || !user.getState().equals("teacher"))
	response.sendRedirect("login.jsp");

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
  	<base href="<%=basePath%>"/>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache,must-revalidate"/>
	<meta http-equiv="expires" content="0"/>
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"/>
	<meta http-equiv="description" content="Upload resource to Software Engineering Course"/>
	<title>Upload resource to Software Engineering Course</title>
	<link rel="stylesheet" type="text/css" href="css/backpage.css" />
	<script type="text/javascript" src="js/default.js"></script>
	<script type="text/javascript">
		function onBack(){
			var resType = "<%=request.getParameter("resType")%>";
			if( resType == "all" ) resType = "doc";
			(document.getElementById("uploadForm")).action = "../itemlist.jsp?resType=" + resType;
			(document.getElementById("uploadForm")).submit();
		}
		function onSubmit(){
			if( document.getElementById("file").value != "" ){
				document.getElementById("hiddenDescrip").value =
					document.getElementById("description").value.replace(/\n/g,"<br/>"); //input 域无法插入\n,故转换为<br/>
				(document.getElementById("uploadForm")).submit();
			}else alert("Please select a file to upload~~");
		}
		function onErrorBack(sec){
			document.getElementById("skip").innerHTML = sec;
			if( sec-- > 0 )
				setTimeout( "onErrorBack("+sec+")" , 1000 );
			else
				window.location.href=
					"<%=basePath%>jsp/upload.jsp?resType=<%=request.getParameter("resType")%>&descript=<%=descripInAttr%>";
		}
		function countSkip(sec, success){
			document.getElementById("skip").innerHTML = sec;
			if( sec-- > 0 )
				setTimeout( "countSkip("+sec+",'"+success+"')" , 1000 );
			else if( success == "true" )
				window.location.href = "<%=basePath%>../itemlist.jsp?resType=<%=request.getParameter("resType")%>";
			else
				window.location.href=
					"<%=basePath%>jsp/upload.jsp?resType=<%=request.getParameter("resType")%>&descript=<%=descripInAttr%>";
		}
	</script>
  </head>
  
  <body>
	
	<div id = "Header">
		
		<div id = "Logon">
			<form action="">
				<table>
					<tr>
						<td> 欢迎您<s:property value="#session.userInfo.getName()" /> </td>
						<td class="seperator"></td>
						<td> <a href="../backlogin!logout.action">退出</a> </td>
					</tr>
				</table>
			</form>
		</div>
		
		<div id = "Logo">
				<img src="<%=basePath%>img/logo.png" alt="南开大学" />
		</div>
		
		<div id = "Navigation">		
			<table>
				<tr>
					<td id="nav_doc">
							<a href="jsp/upload.jsp?resType=doc" style="font-family: '微软雅黑';">教学课件</a>
						</td>
						<td id="nav_video">
							<a href="jsp/upload.jsp?resType=video" style="font-family: '微软雅黑';">教学视频</a>
						</td>
						<td id="nav_software">
							<a href="jsp/upload.jsp?resType=software" style="font-family: '微软雅黑';">教学软件</a>
						</td>
						<td id="nav_reference">
							<a href="jsp/upload.jsp?resType=reference" style="font-family: '微软雅黑';">参考资料</a>
						</td>
						<td id="nav_notice">
							<a href="../notices!showNotices.action" style="font-family: '微软雅黑';">课程通知</a>
						</td>
						<td id="nav_exam">
							<a href="jsp/addexam.jsp" style="font-family: '微软雅黑';">题目管理</a>
						</td>
				</tr>
			</table>
			<script type="text/javascript">
				navClicked("<%= request.getParameter("resType") %>");
			</script>
			
		</div>
	</div>
	
	<div id = "MainContent">
		
		<div id = "TopLine"></div>

		<table>
			<tr>
		
				<td valign="top" class="content_left">
					<div id = "Menu">
						<div id = "MenuTop"></div>
						<div id = "MenuMid">
							
							<div id = "MenuUnit" class="search">
							  <ul>
								<li>检索字段 <input id="keyWord" name="keyWord" type="text" disabled="disabled" /></li>
								<li>
									字段类型
									<select id="wordType" name="wordType" disabled="disabled">
										<option value="all">所有</option>
									</select>
								</li>
								<li>
									检索范围
									<select id="searchScope" name="searchScope" disabled="disabled">
										<option >当前选项卡</option>
									</select>
								</li>								
								<li class="center"> <input type="submit" onclick="onSearch()" value="检索" disabled="disabled"/>	</li>
							  </ul>
							</div>							
								
							<div id="MenuUnit" class="catalogue">
								<ul><li> <a href="">默认归档</a> </li></ul>
							</div>
					</div>
				</td>
		
				<td valign="top" class="content_right">
				 <%	if( request.getAttribute("status") == null ){%>
				  <form id="uploadForm" enctype="multipart/form-data" action="../onUpload.servlet?resType=<%=request.getParameter("resType")%>" method="post">
					<table>
						<tr>
							<td>
								<input type="text" id="hiddenDescrip" name="hiddenDescrip" style="visibility: hidden;"/>
								<br/>选择文件 <br/>
								<input type="file" id="file" name="file" value="abc" style="visibility: ; width:;" /><!--onchange="fakeFile.value=this.value"/-->
								<br/>
								<div style="margin:0; padding:0; color:#0066FF; font-size:13px;">
									&nbsp&nbsp&nbsp 文件大小限制：&nbsp25M<br/>
									&nbsp&nbsp&nbsp 支持文件类型： <%=UploadHandler.instance.getExtensAllowed()%>
								</div>
								<br/>文件描述  <br/>
								<textarea id="description" name="description" cols="40" rows="10" ><%=descripInParam%></textarea>
								<p></p>
							</td>
						</tr>
						<tr>
							<td align="right">
								<input type="button" id="submitButton" value="提交" onclick="onSubmit()" />
								&nbsp;&nbsp;&nbsp;&nbsp;
								<input type="button" id="backButton" value="返回" onclick="onBack()"/>
								<p></p>
							</td>
						</tr>
					</table>
				  </form>
				 <% 
				  	}else{
					  	int status = (Integer)request.getAttribute("status"); 
			 	  		if( status == UploadHandler.SUCCESS ){
			 	 %>
				 	<br/><br/>
				 	<a href="javascript:countSkip(0,'true')" style="color:#0000FF; cursor:pointer; text-decoration:underline;">
				 		<p>上传成功！！！</p>
				 		页面<span id="skip">3</span>秒后自动跳转...<br/>
				 		<br/>如果未自动跳转，请点击此处手动跳转
				 	</a>
				 	<script type="text/javascript">countSkip(3,"true");</script>
				 	
				 <% 	}else{ %>
				 	<br/><br/>
				 	<a  href="javascript:countSkip(0,'false')" style="color:#FF0000; cursor:pointer; text-decoration:underline;">
				 <% 		if( status == UploadHandler.ERROR_UNSPT_TYPE ){ %>
				 		上传失败：不支持该类型文件上传！！！<br/><br/>
				 <% 		}else if( status == UploadHandler.ERROR_LARGE ){ %>
				 		上传失败：文件超过指定大小！！！<br/><br/>
				 <% 		}else if( status == UploadHandler.ERROR_EXISTS ){ %>
				 		上传失败：同名文件已存在！！！<br/><br/>
				 <% 		}else if( status == UploadHandler.ERROR_UNSPT_TYPE ){ %>
				 		上传失败：未知错误发生！！！<br/><br/>
				 <% 		} %>
				 		页面<span id="skip">3</span>秒后自动跳转...<br/><br/>
				 		如果未自动跳转，请点击此处手动跳转
				 	</a>
				 	<script type="text/javascript">countSkip(3,"false");</script>
				 <% }} %>
				</td>
			</tr>
		</table>
		
		<div id = "BottomLine"></div>
		
	</div>
	
	<div id = "Footer">
		<table width="100%">
			<tr>
				<td width=33% align="center" valign="middle">
					<p style="color:#0066FF;">天津南开大学软件学院开发制作</p>
					<p style="color:#0066FF;">&nbsp;&nbsp;&nbsp;&nbsp;EDUCATIONAL&nbsp;TECHNOLOGY</p>
				</td>
				<td width=34% align="center" valign="middle">
					<img src="<%=basePath%>img/logo_nk.png" alt="南开大学" />
				</td>
				<td width=33% align="center" valign="middle">
					<p style="color:#0066FF;">版权所有@南开大学软件学院</p>
				</td>
			</tr>
		</table>
	</div>

  </body>
</html>